var io = require('socket.io').listen(8088, {
    log: true
});
console.log('server start');

var sockets = [];

io.sockets.on('connection', function(socket) {
    socket.on("join", function(name) {
        if (sockets[name]) {
            return;
        }
        for (nameObj in sockets) {
            sockets[nameObj].emit("message", {
                'author': 'sys',
                'content': '新成员加入聊天室!',
                'time': /\d{4}\s.*?\s/.exec(new Date())[0]
            });
        }
        sockets[name] = socket;
        console.log("新成员加入：" + name);

    });

    socket.on("reciver", function(data) {
        console.log(data);
        for (nameObj in sockets) {
            sockets[nameObj].emit("message", data);
        }
    });

    socket.emit("message", {
        'author': 'sys',
        'content': '欢迎加入聊天室!',
        'time': /\d{4}\s.*?\s/.exec(new Date())[0]
    });
});